﻿using Common.Service;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RabitPart.ReadApi.Application.Loginfile;
using RabitPart.ReadApi.Application.RBACrelationship.RELUserShow;
using RabitPart.ReadApi.Application.RBACrelationship.Relroleperm; 
using RabitPart.ReadApi.Utils;

namespace RabitPart.ReadApi.Controllers
{
    /// <summary>
    /// 控制器--读api
    /// </summary>
    [Route("api/[controller]/[action]")]

    [ApiController]
    [Authorize]
    public class RbacReadAllController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<RbacReadAllController> logger;
        private readonly SmsClientFactory _smsClientFactory;

        public RbacReadAllController(IMediator mediator, ILogger<RbacReadAllController> logger, SmsClientFactory _smsClientFactory)
        {
            this.mediator = mediator;
            this.logger = logger;
            this._smsClientFactory = _smsClientFactory;
        }

        /// <summary>
        /// 异常处理
        /// </summary>
        /// <returns>异常处理</returns>
        [HttpGet]
        public IActionResult GetText()
        {
            try
            {
                int a = 0;
                int b = 10 / a;
                return Ok("ok");
            }
            catch (Exception ex)
            {
                logger.LogError("异常处理" + ex.Message);
                logger.LogInformation("异常处理" + ex.Message);
                throw; 
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userpass">密码</param>
        /// <returns>登录</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<LoginQueryCommanResponse>> GetLogin(string? userName, string? userpass)
        {
            try
            {
                LoginQueryComman model = new LoginQueryComman();
                model.userpass = userpass;
                model.userName = userName;
                return await mediator.Send(model);
            }
            catch (Exception ex)
            {
                logger.LogError("登录失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 阿里云短信测试
        /// </summary>
        [HttpGet("sendSms")]
        public void SendSms()
        {
            _smsClientFactory.SendSms("15831827870", "{\"code\":\"666666\"}", "阿里云短信测试", "SMS_154950909");
        }


        /// <summary>
        /// 用户角色显示
        /// </summary>
        /// <param name="model">s实体</param>
        /// <returns>用户角色显示</returns>
        [HttpGet]
        public async Task<ApiResult<IList<ShowRel_user_roleCommanResponse>>> ShowUserRole()
        {
            try
            {
                ShowRel_user_roleComman model = new ShowRel_user_roleComman();
                return await mediator.Send(model);
            }
            catch (Exception ex)
            {
                logger.LogError("用户角色显示异常" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 用户显示
        /// </summary>
        /// <param name="model">s实体</param>
        /// <returns>用户显示</returns>
        [HttpGet]
        public async Task<ApiResult<IList<UserShowCommanResponstity>>> userShow()
        {
            try
            {
                UserShowComman model = new UserShowComman();
                return await mediator.Send(model);
            }
            catch (Exception ex)
            {
                logger.LogError("用户显示异常" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 递归
        /// </summary> 
        /// <returns>递归</returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetTreeRolePermCommanResponstity>>> GETDTOpermission( )
        {
            try
            {
                GetTreeRolePermComman model = new GetTreeRolePermComman();
                return await mediator.Send(model);
            }
            catch (Exception ex)
            {
                logger.LogError("递归异常" + ex.Message);
                throw;
            }
        }




    }
}
